<?php

namespace App\Http\Controllers\EnquiryQuote;

use Illuminate\Http\Request;

use App\Enquiry;
use App\EnquiryReply;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use URL;
use Validator;
use DB;


class EnquiryQuoteController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $datetime = date('Y-m-d H:i:s');
        $current_user = auth()->user();
        $current_supplier = $current_user->supplier;
        $supply_goods = json_decode($current_supplier->goods, true);

        $enquiries = Enquiry::whereIn('goods_id', $supply_goods);
        $enquiries = $enquiries->orderBy('created_at', 'desc');
        $enquiries = $enquiries->where('start_at', '<=', $datetime);
        $enquiries = $enquiries->paginate(10);
        $pages = $enquiries->appends([])->render();
        return view('enquiry_quote.enquiry_quote_list')
            ->with('enquiries', $enquiries)
            ->with('pages', $pages)
            ->with('datetime', $datetime);
    }

    /**
     *
     */
    public function view(Enquiry $enquiry)
    {
        $current_user = auth()->user();
        $datetime = date('Y-m-d H:i:s');
        $my_reply = $enquiry->replies()->where('supplier_id', $current_user->id)->first();


        return view('enquiry_quote.enquiry_quote_list_detail')
            ->with('my_reply', $my_reply)
            ->with('enquiry', $enquiry)
            ->with('datetime', $datetime);
    }

    public function pending(Enquiry $enquiry)
    {
        $current_user = auth()->user();
        $datetime = date('Y-m-d H:i:s');

        return view('enquiry_quote.enquiry_quote_detail')
            ->with('enquiry', $enquiry)
            ->with('datetime', $datetime);
    }

    public function reply(Request $request, Enquiry $enquiry)
    {
        $redirect_url = route('enquiryQuote.eqDetail', ['enquiry' => $enquiry->id]);
        $datetime = date('Y-m-d H:i:s');
        $supplier_id = auth()->user()->id;
        if ($enquiry->stop_at < $datetime) {
            return redirect($redirect_url)->with('tip_message', ['content' => '抱歉，报价已结束', 'state' => 'warning']);
        } else {
            if ($enquiry->replies()->where('supplier_id', $supplier_id)->exists()) {
                return redirect($redirect_url)->with('tip_message', ['content' => '已经报过价了', 'state' => 'warning']);
            } else {
                $form_data = $request->only([
                    'quality', 'payment', 'price', 'remark', 'price_validity'
                ]);
                $form_data = array_map('trim', $form_data);
                $form_data['quality'] = strip_tags($form_data['quality']);
                $form_data['payment'] = strip_tags($form_data['payment']);
                $form_data['remark'] = strip_tags($form_data['remark']);

                $rules = [
                    'quality'     => 'required',
                    'price_validity' => 'required|numeric|min:0',
                    'price'  => 'required|numeric|min:0',
                    'payment' => 'required',
                ];
                $messages = [
                    'required' => '必填项不能为空',
                    'numeric'  => '请填写数字',
                    'price.min' => '价格必须大于0',
                ];
                $validator = Validator::make($form_data, $rules, $messages);
                if ($validator->fails()) {
                    return redirect($redirect_url)->withErrors($validator->errors())->withInput();
                } else {
                    $enquiry_reply_data = $form_data;
                    $enquiry_reply_data['supplier_id'] = $supplier_id;
                    $enquiry_reply_data['enquiry_id'] = $enquiry->id;
                    if (EnquiryReply::create($enquiry_reply_data)) {

                        $redirect_url = route('enquiryQuote.eqList');

                        return redirect($redirect_url)->with('tip_message', ['content' => '询价单报价成功', 'state' => 'success']);
                    } else{
                        return redirect($redirect_url)->withErrors(['form' => ['服务器繁忙，请稍后再试']]);
                    }
                }
            }
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //


    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
